// app/model/bank_transaction_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DECIMAL, DATE, TEXT, BOOLEAN, ENUM } = app.Sequelize;

  const BankTransaction = app.model.define('bankTransaction', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    organizationId: {
      type: INTEGER,
      allowNull: false,
      field: 'organization_id',
      comment: '组织ID'
    },
    transactionId: {
      type: STRING(200),
      allowNull: false,
      unique: true,
      field: 'transaction_id',
      comment: '流水号'
    },
    payerName: {
      type: STRING(200),
      allowNull: false,
      field: 'payer_name',
      comment: '付款方名称'
    },
    payerAccount: {
      type: STRING(100),
      field: 'payer_account',
      comment: '付款方账号'
    },
    amount: {
      type: DECIMAL(12, 2),
      allowNull: false,
      comment: '交易金额'
    },
    transactionDate: {
      type: DATE,
      allowNull: false,
      field: 'transaction_date',
      comment: '交易日期'
    },
    transactionTime: {
      type: DATE,
      allowNull: false,
      field: 'transaction_time',
      comment: '交易时间'
    },
    balance: {
      type: DECIMAL(12, 2),
      comment: '交易后余额'
    },
    remark: {
      type: TEXT,
      comment: '交易备注'
    },
    dataSource: {
      type: ENUM('file_import', 'plugin_capture'),
      defaultValue: 'file_import',
      field: 'data_source',
      comment: '数据来源'
    },
    importBatch: {
      type: STRING(100),
      field: 'import_batch',
      comment: '导入批次号'
    },
    isFiltered: {
      type: BOOLEAN,
      defaultValue: false,
      field: 'is_filtered',
      comment: '是否已过滤'
    },
    filterReason: {
      type: STRING(100),
      field: 'filter_reason',
      comment: '过滤原因'
    },
    createdAt: {
      type: DATE,
      field: 'created_at'
    },
    // 新增字段
    bankName: {
      type: STRING(200),
      field: 'bank_name',
      comment: '银行名称'
    },
    currency: {
      type: STRING(50),
      comment: '币种'
    },
    channel: {
      type: STRING(50),
      comment: '交易渠道'
    },
    fee: {
      type: DECIMAL(12, 2),
      defaultValue: 0,
      comment: '手续费'
    },
    accountName: {
      type: STRING(200),
      field: 'account_name',
      comment: '己方账户名称'
    },
    accountNumber: {
      type: STRING(100),
      field: 'account_number',
      comment: '己方账户号码'
    }
  }, {
    tableName: 'bank_transactions',
    timestamps: false
  });

  // 添加关联关系
  BankTransaction.associate = function() {
    // 银行流水与组织的关联
    if (app.model.OrganizationModel) {
      BankTransaction.belongsTo(app.model.OrganizationModel, {
        foreignKey: 'organizationId',
        as: 'organization'
      });
    }
  };

  return BankTransaction;
};